#include <ctime>
#include "function.h"
#include "util.h"

namespace sp{

std::string Util::currentDateToString()
{
	time_t rawtime;
	struct tm * timeinfo;
	char buffer[80];
	time(&rawtime);
	timeinfo = localtime(&rawtime);
	strftime(buffer, sizeof(buffer), "%Y_%m_%d_%H_%M_%S", timeinfo);
	std::string str(buffer);
	return str;
}

void Util::genBarChart(std::string title, std::unordered_map<std::string, double> data){
    
}

void Util::ordinaryLeastSquares(std::vector<sp::Record> Records, int& k, int& b){

}

pid_t Util::gettid() {
	return syscall(SYS_gettid);
}

int Util::ToWchar(char* &src, wchar_t* &dest, const char* locale){
    if (src == NULL) {
        dest = NULL;
        return 0;
    }

    // 根据环境变量设置locale
    setlocale(LC_CTYPE, locale);

    // 得到转化为需要的宽字符大小
    int w_size = mbstowcs(NULL, src, 0) + 1;

    // w_size = 0 说明mbstowcs返回值为-1。即在运行过程中遇到了非法字符(很有可能使locale
    // 没有设置正确)
    if (w_size == 0) {
        dest = NULL;
        return -1;
    }

    //wcout << "w_size" << w_size << endl;
    dest = new wchar_t[w_size];
    if (!dest) {
        return -1;
    }

    int ret = mbstowcs(dest, src, strlen(src)+1);
    if (ret <= 0) {
        return -1;
    }
    return 0;
}

std::string Util::DataTypeToString(uint8_t type){
    if(type == 0x00){
        return "unknow";
    }else if(type == 0x01){
        return "head";
    }else if(type == 0x02){
        return "mat";
    }else if(type == 0x03){
        return "rect";
    }else if(type == 0x04){
        return "tensor";
    }else if(type == 0x05){
        return "string";
    }else if(type == 0x06){
        return "batch tensor";
    }else if(type == 0x07){
        return "gpu tensor";
    }else if(type == 0x08){
        return "batch gpu tensor";
    }else{
        assert(false);
    }
}

std::string Util::join(std::vector<std::string>& sequence, std::string separator){
    std::string result;
	for (size_t i = 0; i < sequence.size(); ++i)
		result += sequence[i] + ((i != sequence.size() - 1) ? separator : "");
	return result;
}

};